package com.hyphenate.easeui.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.IdRes;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chatuidemo.widget.ParentViewPager;
import com.hyphenate.easeui.domain.EaseEmojicon;
import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity;
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.EaseChatExtendMenu.EaseChatExtendMenuItemClickListener;
import com.hyphenate.easeui.widget.EaseChatPrimaryMenuBase.EaseChatPrimaryMenuListener;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase.EaseEmojiconMenuListener;
import com.hyphenate.util.PathUtil;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.EaseChatMenuAdapter;
import com.joplus.housekeeper.utils.T;
import com.joplus.housekeeper.utils.UMengUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 聊天页面底部的聊天输入菜单栏 <br/>
 * 主要包含3个控件:EaseChatPrimaryMenu(主菜单栏，包含文字输入、发送等功能), <br/>
 * EaseChatExtendMenu(扩展栏，点击加号按钮出来的小宫格的菜单栏), <br/>
 * 以及EaseEmojiconMenu(表情栏)
 */
public class EaseChatInputMenu extends LinearLayout {

    FrameLayout primaryMenuContainer;//工具


    protected EaseChatPrimaryMenuBase chatPrimaryMenu;


    protected LayoutInflater layoutInflater;
    private LinearLayout layout;

    private Handler handler = new Handler();
    public ChatInputMenuListener listener;
    private Context context;
    private boolean inited;
    private RadioGroup rg;
    private ParentViewPager vp;
    private EaseChatMenuAdapter easeChatMenuAdapter;
    private LinearLayout ll_container;
    private TabLayout tab;

    public EaseChatInputMenu(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public EaseChatInputMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public EaseChatInputMenu(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.ease_widget_chat_input_menu, this);
        vp = (ParentViewPager) findViewById(R.id.vp);

        TabLayout tablayout = (TabLayout) findViewById(R.id.tablayout);
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        View view_other = layoutInflater.inflate(R.layout.item_chat_other, null);
        View view_rabit = layoutInflater.inflate(R.layout.item_chat_rabit, null);
        View view_emoji = layoutInflater.inflate(R.layout.item_chat_emoji, null);
        List<View> objects = new ArrayList<>();
        if (objects.size() != 3) {
            objects.add(view_other);
            objects.add(view_rabit);
            objects.add(view_emoji);
            easeChatMenuAdapter = new EaseChatMenuAdapter(this, objects);
            vp.setAdapter(easeChatMenuAdapter);
        }
        primaryMenuContainer = (FrameLayout) findViewById(R.id.primary_menu_container);

        tablayout.setupWithViewPager(vp);
        for (int i = 0; i < tablayout.getTabCount(); i++) {
            tablayout.getTabAt(i).setCustomView(easeChatMenuAdapter.getTabView(i));
        }


    }

    /**
     * init view 此方法需放在registerExtendMenuItem后面及setCustomEmojiconMenu，
     * setCustomPrimaryMenu(如果需要自定义这两个menu)后面
     *
     * @param emojiconGroupList 表情组类别，传null使用easeui默认的表情
     */
    public void init(List<EaseEmojiconGroupEntity> emojiconGroupList) {

        if (inited) {
            return;
        }


        // 主按钮菜单栏,没有自定义的用默认的
        if (chatPrimaryMenu == null) {
            chatPrimaryMenu = (EaseChatPrimaryMenu) layoutInflater.inflate(R.layout.ease_layout_chat_primary_menu, null);
        }
        primaryMenuContainer.addView(chatPrimaryMenu);
        // 表情栏，没有自定义的用默认的


      /*  sudokuMenuContainer.removeAllViews();
        layout = (LinearLayout) layoutInflater.inflate(R.layout.include_chat_sudoku, null);
        sudokuMenuContainer.addView(layout);*/


        processChatMenu();
        // 初始化extendmenu

        inited = true;


    }


    public void init() {
        init(null);
    }


    /**
     * 设置自定义的主菜单栏，该控件需要继承自EaseChatPrimaryMenuBase，
     * 以及回调你想要回调出去的事件给设置的EaseEmojiconMenuListener
     *
     * @param customPrimaryMenu
     */
    public void setCustomPrimaryMenu(EaseChatPrimaryMenuBase customPrimaryMenu) {
        this.chatPrimaryMenu = customPrimaryMenu;
    }


    private List<EaseChatExtendMenu.ChatMenuItemModel> menuList = new ArrayList<>();

    /**
     * 注册扩展菜单的item
     *
     * @param nameRes     item名字
     * @param drawableRes item背景
     * @param itemId      id
     * @param listener    item点击事件
     */

    public void registerExtendMenuItem(int nameRes, int drawableRes, int itemId,
                                       EaseChatExtendMenuItemClickListener listener) {
        EaseChatExtendMenu.ChatMenuItemModel item = new EaseChatExtendMenu.ChatMenuItemModel();
        item.name = context.getString(nameRes);
        item.image = drawableRes;
        item.id = itemId;
        item.clickListener = listener;

        menuList.add(item);
      /*  chatExtendMenu.registerMenuItem(nameRes, drawableRes, itemId, listener);
        if (easeChatMenuAdapter != null && easeChatMenuAdapter.getChatExtendMenu() != null) {
            easeChatMenuAdapter.getChatExtendMenu().registerMenuItem(nameRes, drawableRes, itemId, listener);
        }*/

    }

    public void addEmojiListener() {
        // emojicon menu
        if (easeChatMenuAdapter != null && easeChatMenuAdapter.getEmoji() != null) {
            easeChatMenuAdapter.getEmoji().setEmojiconMenuListener(new EaseEmojiconMenuListener() {

                @Override
                public void onExpressionClicked(EaseEmojicon emojicon) {
                    if (emojicon.getType() != EaseEmojicon.Type.BIG_EXPRESSION) {
                        if (emojicon.getEmojiText() != null) {
                            chatPrimaryMenu.onEmojiconInputEvent(EaseSmileUtils.getSmiledText(context, emojicon.getEmojiText()));
                        }
                    } else {
                        if (listener != null) {
                            listener.onBigExpressionClicked(emojicon);
                        }
                    }
                }

                @Override
                public void onDeleteImageClicked() {
                    chatPrimaryMenu.onEmojiconDeleteEvent();
                }
            });

        }
    }

    /**
     * 注册扩展菜单的item
     */

    public void registerExtendMenuItem() {

        //   chatExtendMenu.registerMenuItem(nameRes, drawableRes, itemId, listener);
        for (EaseChatExtendMenu.ChatMenuItemModel chatMenuItemModel : menuList) {
            Log.e("regist========", "registerExtendMenuItem: ");
            if (easeChatMenuAdapter != null && easeChatMenuAdapter.getChatExtendMenu() != null) {
                Log.e("regist========", "registerExtendMenuItem:  doit========");
                easeChatMenuAdapter.getChatExtendMenu().registerMenuItem(chatMenuItemModel);
            }
        }


    }


    protected void processChatMenu() {
        // 发送消息栏
        chatPrimaryMenu.setChatPrimaryMenuListener(new EaseChatPrimaryMenuListener() {

            @Override
            public void onSendBtnClicked(String content) {
                if (listener != null)
                    try {
                        listener.onSendMessage(content);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
            }

            @Override
            public void onToggleVoiceBtnClicked() {
                hideExtendMenuContainer();
            }

            @Override
            public void onToggleExtendClicked() {
               /* if (click != null) {
                    click.camera();
                }*/

                toggleMore();


            }

            @Override
            public void onToggleEmojiconClicked() {

                toggleEmojicon();

            }

            @Override
            public void onEditTextClicked() {
                hideExtendMenuContainer();
            }


            @Override
            public boolean onPressToSpeakBtnTouch(View v, MotionEvent event) {
                if (listener != null) {
                    return listener.onPressToSpeakBtnTouch(v, event);
                }
                return false;
            }
        });


    }

    /**
     * 显示或隐藏图标按钮页
     */
    protected void toggleMore() {
        if (ll_container.getVisibility() == View.GONE) {
            hideKeyboard();
            ll_container.setVisibility(View.VISIBLE);
        } else {
            ll_container.setVisibility(View.GONE);
        }

       /* if (emojiconMenu.getVisibility() == View.GONE) {
            hideKeyboard();
            handler.postDelayed(new Runnable() {
                public void run() {
                    chatExtendMenuContainer.setVisibility(View.VISIBLE);
                    chatExtendMenu.setVisibility(View.VISIBLE);
                    emojiconMenu.setVisibility(View.VISIBLE);
                //    layout.setVisibility(View.GONE);
                }
            }, 50);
        } else {
            if (emojiconMenu.getVisibility() == View.VISIBLE) {
                emojiconMenu.setVisibility(View.GONE);
                chatExtendMenu.setVisibility(View.VISIBLE);
            } else {
                chatExtendMenuContainer.setVisibility(View.GONE);

            }*/
           /* if (layout.getVisibility() == View.VISIBLE) {
                layout.setVisibility(View.GONE);
                chatExtendMenu.setVisibility(View.VISIBLE);
            } else {
                chatExtendMenuContainer.setVisibility(View.GONE);
            }

        }
*/
    }


    /**
     * 显示或隐藏表情页  其实这里是九宫格！原来的coder用表情的布局实现了九宫格
     */
    protected void toggleEmojicon() {
      /*  emojiconMenuContainer.setVisibility(View.INVISIBLE);
        if (chatExtendMenuContainer.getVisibility() == View.GONE) {
            hideKeyboard();
            handler.postDelayed(new Runnable() {
                public void run() {
                    chatExtendMenuContainer.setVisibility(View.VISIBLE);
                    chatExtendMenu.setVisibility(View.GONE);
                    emojiconMenu.setVisibility(View.VISIBLE);
                    //        layout.setVisibility(View.VISIBLE);
                }
            }, 50);
        } else {
            if (emojiconMenu.getVisibility() == View.VISIBLE) {
                chatExtendMenuContainer.setVisibility(View.GONE);
                emojiconMenu.setVisibility(View.GONE);
            } else {
                chatExtendMenu.setVisibility(View.GONE);
                emojiconMenu.setVisibility(View.VISIBLE);
            }
*/
        if (ll_container.getVisibility() == View.GONE) {
            //     emojiconMenuContainer.setVisibility(VISIBLE);
            ll_container.setVisibility(View.VISIBLE);
        } else {

            ll_container.setVisibility(GONE);
        }
           /* if (layout.getVisibility() == View.VISIBLE) {
                chatExtendMenuContainer.setVisibility(View.GONE);
                layout.setVisibility(View.GONE);
            } else {
                chatExtendMenu.setVisibility(View.GONE);
                layout.setVisibility(View.VISIBLE);
            }


    }*/

    }

    /**
     * 显示/隐藏 九宫格 其实这里是显示隐藏表情！！
     */
    private void toggleSuduko() {

        if (ll_container.getVisibility() == View.GONE) {
            hideKeyboard();
            handler.postDelayed(new Runnable() {
                public void run() {
                   /* chatExtendMenuContainer.setVisibility(View.VISIBLE);
                    chatExtendMenu.setVisibility(View.GONE);
                    emojiconMenu.setVisibility(View.VISIBLE);*/
                    //   layout.setVisibility(View.VISIBLE);
                    ll_container.setVisibility(View.VISIBLE);
                }
            }, 50);
        } else {
            ll_container.setVisibility(View.VISIBLE);

            //    chatExtendMenu.setVisibility(View.GONE);
            //   emojiconMenu.setVisibility(VISIBLE);

        }

    }


    /**
     * 显示表情
     */
    public void showEmoji() {
        toggleSuduko();
    }

    /**
     * 隐藏软键盘
     */
    private void hideKeyboard() {
        chatPrimaryMenu.hideKeyboard();
    }

    /**
     * 隐藏整个扩展按钮栏(包括表情栏)
     */
    public void hideExtendMenuContainer() {

        // emojiconMenu.setVisibility(View.GONE);
        //  layout.setVisibility(View.GONE);
        //   chatExtendMenuContainer.setVisibility(View.GONE);
        //  chatExtendMenu.setVisibility(View.GONE);
        ll_container.setVisibility(GONE);
        chatPrimaryMenu.onExtendMenuContainerHide();

    }

    /**
     * 系统返回键被按时调用此方法
     *
     * @return 返回false表示返回键时扩展菜单栏时打开状态，true则表示按返回键时扩展栏是关闭状态<br/>
     * 如果返回时打开状态状态，会先关闭扩展栏再返回值
     */
    public boolean onBackPressed() {
        if (ll_container.getVisibility() == View.VISIBLE) {
            hideExtendMenuContainer();
            return false;
        } else {
            return true;
        }

    }


    public EditText getEditText() {
        return ((EaseChatPrimaryMenu) chatPrimaryMenu).getEditText();
    }

    public void setChatInputMenuListener(ChatInputMenuListener listener) {
        this.listener = listener;
    }

    RightClick click;

    public void setRightClick(RightClick click) {
        this.click = click;
    }

    public interface RightClick {
        void camera();
    }

    public interface ChatInputMenuListener {
        /**
         * 发送消息按钮点击
         *
         * @param content 文本内容
         */
        void onSendMessage(String content) throws UnsupportedEncodingException;

        /**
         * 大表情被点击
         *
         * @param emojicon
         */
        void onBigExpressionClicked(EaseEmojicon emojicon);

        /**
         * 长按说话按钮touch事件
         *
         * @param v
         * @param event
         * @return
         */
        boolean onPressToSpeakBtnTouch(View v, MotionEvent event);


        void onGifClick(String iconCode);
    }


}
